import time,os
from contents.utils import get_categories
from .models import ContentCategory
from django.shortcuts import render
from django.template import loader
from django.conf import settings





def genterate_static_index_html():
    '''生成首页静态文件'''
    print('%s : genterate_static_index_html' % time.ctime())
    categorieds = get_categories()
    contents = {}
    contents_categories = ContentCategory.objects.all()
    for cat in contents_categories:
        contents[cat.key] =cat.content_set(status=True).order_by('sequence')


    context = {
        'categories' : categorieds,
        'contents': contents

    }




    template = loader.get_template('index.html')
    html_text = template.render(context)

    file_path = os.path.join(settings.STATICFILES_DIRS[0], 'index.html')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html_text)




